import { query, mutation, baseImgUrl } from '@/utils/http'

function apiGetOrders(token, params) {
  const str = `
  ($request:OrderListRequest!){
    orderList(request:$request){
      page{
        hasNext
      }
      datas{
        rownum
        orderNumber
        travelStatus
        travelDate
        totalAmount
        route{
          id
          name
          createTime
          picture
        }
        routeProcessStatus
        orderRoutePrice{
          id
          thatDate
          singlePersonPrice
        }
      }
    }
  }
  `
  return query(token, str, { request: params })
}

function apiCancelOrder(token, orderNumber) {
  const str = `
    ($orderNumber:String!){
      cancelOrder(orderNumber:$orderNumber)
    }
  `
  return mutation(token, str, { orderNumber })
}

// 下单后未支付,重新支付的订单
function apiRepayOrder(token, orderNumber) {
  const str = `
    ($request:TravelRouteOrderPayInput!){
      payTravelRouteOrder(request:$request){
        timeStamp
        nonceStr
        payPackage
        paySign
        signType        
      }
    }
  `
  return mutation(token, str, { request: { orderNumber } })
}

function apiGetOrderDetail(token, orderNumber) {
  const str = `
    ($orderNumber:String!){
      orderDetail(orderNumber:$orderNumber){
        orderNumber
        createTime
        route{
          id
          name
          picture
          labelList
          isVisa
        }
        totalAmount
        travelDate
        orderRoutePrice{
          id
          singlePersonPrice
          singlePersonQuantity
          twoPeoplePrice
          twoPeopleQuantity
          fourPeoplePrice
          fourPeopleQuantity
          sixPeoplePrice
          sixPeopleQuantity
          eightPeoplePrice
          eightPeopleQuantity
          singleRoomPrice
          roomQuantity
        }
      }
    }
  `
  return new Promise((resolve, reject) => {
    query(token, str, { orderNumber })
      .then(res => {
        if (res.data.data && res.data.data.orderDetail) {
          const result = res.data.data.orderDetail
          const packages = []
          if (result.orderRoutePrice.singlePersonQuantity) {
            packages.push({
              name: '单人套餐',
              quantity: result.orderRoutePrice.singlePersonQuantity,
              price:
                result.orderRoutePrice.singlePersonPrice *
                result.orderRoutePrice.singlePersonQuantity
            })
          }
          if (result.orderRoutePrice.twoPeopleQuantity) {
            packages.push({
              name: '双人套餐',
              quantity: result.orderRoutePrice.twoPeopleQuantity,
              price:
                result.orderRoutePrice.twoPeoplePrice * result.orderRoutePrice.twoPeopleQuantity
            })
          }
          if (result.orderRoutePrice.fourPeopleQuantity) {
            packages.push({
              name: '四人套餐',
              quantity: result.orderRoutePrice.fourPeopleQuantity,
              price:
                result.orderRoutePrice.fourPeoplePrice * result.orderRoutePrice.fourPeopleQuantity
            })
          }
          if (result.orderRoutePrice.sixPeopleQuantity) {
            packages.push({
              name: '六人套餐',
              quantity: result.orderRoutePrice.sixPeopleQuantity,
              price:
                result.orderRoutePrice.sixPeoplePrice * result.orderRoutePrice.sixPeopleQuantity
            })
          }
          if (result.orderRoutePrice.eightPeopleQuantity) {
            packages.push({
              name: '八人套餐',
              quantity: result.orderRoutePrice.eightPeopleQuantity,
              price:
                result.orderRoutePrice.eightPeoplePrice * result.orderRoutePrice.eightPeopleQuantity
            })
          }
          if (result.orderRoutePrice.roomQuantity) {
            packages.push({
              name: '单房',
              quantity: result.orderRoutePrice.roomQuantity,
              price: result.orderRoutePrice.singleRoomPrice * result.orderRoutePrice.roomQuantity
            })
          }
          resolve({
            orderNumber: result.orderNumber,
            travelDate: result.travelDate,
            createTime: result.createTime,
            totalAmount: result.totalAmount,
            name: result.route.name,
            label: result.route.labelList ? result.route.labelList[0] : '',
            isVisa: result.route.isVisa,
            picture: `${baseImgUrl}${result.route.picture}`,
            packages
          })
        } else {
          reject(new Error('fail'))
        }
      })
      .catch(err => reject(err))
  })
}

export { apiGetOrders, apiCancelOrder, apiRepayOrder, apiGetOrderDetail }
